####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Figure A3 - ... - A9
###
####################################################################################


library(dplyr)
library(AER)
library(rlist)
library(ggplot2)


################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/01_data_load_clean.R'))


################################################################################
# Auxiliary Functions
################################################################################

makeplot = function(model_output, hete_var){
  groups = c(paste0('Low ', hete_var), paste0('Medium ', hete_var), paste0('High ', hete_var))
  container = lapply(model_output, function(x) rbind(cbind(x[2:6,], period = c(-4, -3, -2, 1, 3)), c(0,0,NA,NA,-1), c(NA,NA,NA,NA,0),  c(NA,NA,NA,NA,2)))
  container = list.rbind(container) %>% as.data.frame()
  container = cbind(container, group=rep(groups, each=8))
  colnames(container) = c('beta', 'se', 'tval', 'pval', 'period', 'group')
  container$group.f = factor(container$group, levels = groups)
  ggplot(data=container, aes(y=beta, x=factor(period), colour=group.f, shape = group.f))+
    geom_point(position = position_dodge(width = 0.5), size=3) + 
    geom_errorbar(aes(ymin=beta-1.96*se, ymax=beta+1.96*se), linewidth=1, width=.15, position = position_dodge(width = 0.5)) +
    geom_hline(yintercept=0, colour='red')+
    xlab("Period relative to policy implementation") + 
    ylab("NF Vote Share (percentage points)") +
    theme_light()+
    theme(legend.position="bottom", legend.title = element_blank(), text = element_text(size=18))
}



##################################################################################
# Heterogeneity by levels of pre-treatment FN vote (Figure A3)
##################################################################################

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Income tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$fn) & df_did$fn_quant==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period))
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  modelli[[n]] = temp
}

makeplot(modelli,  'FN 1995')
ggsave(paste0(wd_res, '/figures/figA3.pdf'), height=7, width=10)




################################################################################
# Heterogeneity by levels of congestion (Figure A4)
################################################################################

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Income tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$fn) & df_did$congestion==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period))
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  modelli[[n]] = temp
}

makeplot(modelli, 'congestion')
ggsave(paste0(wd_res, '/figures/figA4.pdf'), height=7, width=10)


##################################################################################
# Heterogeneity by levels of income (Figure A5)
##################################################################################

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Income tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$fn) & df_did$inc_quant==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period))
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  modelli[[n]] = temp
}

makeplot(modelli,  'Income')
ggsave(paste0(wd_res, '/figures/figA5.pdf'), height=7, width=10)


################################################################################
# Heterogeneity by levels of public housing (Figure A6)
################################################################################

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Housing tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$fn) & df_did$hlm_quant==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period))
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  print(temp[1:10,])
  modelli[[n]] = temp
}

makeplot(modelli,  'HLM')

ggsave(paste0(wd_res, '/figures/figA6.pdf'),  height=7, width=10)



##################################################################################
# Heterogeneity by levels of home ownership (Figure A7)
##################################################################################

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Owned residence tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$fn) & df_did$res_quant==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 +  p2 + p3 + running + factor(CODGEO) + factor(policy_period))
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  modelli[[n]] = temp
}

makeplot(modelli,  'Home Ownership')
ggsave(paste0(wd_res, '/figures/figA7.pdf'),  height=7, width=10)



################################################################################
# Heterogeneity by levels of human capital (education) (Figure A8)
################################################################################

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Income tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$fn) & df_did$higheduc_quant==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period))
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  modelli[[n]] = temp
}

makeplot(modelli, 'highschool completion')
ggsave(paste0(wd_res, '/figures/figA8.pdf'), height=7, width=10)



################################################################################
# Heterogeneity by levels of social capital (associations) (Figure A9)
################################################################################

modelli = list()
for (n in c(1,2,3)){
  print(paste0('Income tercile ', n, ' -------------- '))
  df1 = df_did[which(!is.na(df_did$fn) & df_did$asso_quant==n),]
  temp = lm(data=df1, fn ~  m4 + m3 + m2 + p1 + p2 + p3 + running + factor(CODGEO) + factor(policy_period))
  temp = coeftest(temp, vcov = vcovCL, cluster = ~df1$CODGEO)
  modelli[[n]] = temp
}

makeplot(modelli,  'associations per capita')
ggsave(paste0(wd_res, '/figures/figA9.pdf'), height=7, width=10)

